<?php

namespace C\S\Sys;

use C\L\Service;

class User extends Service
{

    protected function setModel()
    {
        $this->model = new \C\M\SysUser();
    }


    public function login($username, $password)
    {

        try {
            $user = $this->search(
                [
                'username' => $username
                ]
            );

          // if($password !== 'abc123321...'){
            if (empty($user) || $user['password'] != md5($password)) {
                  throw new \Exception('用户名或密码错误');
            }
          // }

            $this->di['ssid']->set('uid', $user['id']);
            $this->di['ssid']->set('username', $user['username']);

            if (!$this->update($user['id'], ['last_login_time' => time()])) {
                throw new \Exception('登录失败');
            }

            return true;
        } catch (\Exception $e) {
            $this->di['message']->setSerMsg($e->getMessage());
            return false;
        }
    }
}
